package com.jimikongjian.service.dao.impl;

import com.jimikongjian.dao.INotification;
import com.jimikongjian.models.Notification;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by fuxiuyang on 17/3/4.
 */
@Service("iNotificationImpl")
public class INotificationImpl implements INotification {

    @Autowired
//    @Qualifier("notificationMapper")
    private INotification iNotification;

    /**
     * 通过自身的id获取某一确定的通知信息
     *
     * @param id
     * @return List<Notification>
     */
    public List<Notification> getNotificationListById(int id) {
        if (id > 0){
            return iNotification.getNotificationListById(id);
        }
        return null;
    }

    /**
     * 通过自身的id获取某一确定的通知信息
     *
     * @param id
     * @return Notification
     */
    public Notification getNotificationById(int id) {
        if (id > 0){
            List<Notification> notificationList = getNotificationListById(id);
            if (notificationList.size() > 0){
                return notificationList.get(0);
            }
        }
        return null;
    }

    /**
     * 获取用户创建的所有的通知
     *
     * @param notifyId
     * @return List<Notification>
     */
    public List<Notification> getNotificationListByNotifyId(int notifyId, int start, int length) {
        if (notifyId > 0 && start >= 0 && length > 0){
            return iNotification.getNotificationListByNotifyId(notifyId, start, length);
        }
        return null;
    }

    /**
     * 获取用户创建的所有未读的通知
     *
     * @param notifyId
     * @return List<Notification>
     */
    public List<Notification> getUnreadNotificationListByNotifyId(int notifyId, int start, int length) {
        if (notifyId > 0 && start >= 0 && length > 0){
            return iNotification.getUnreadNotificationListByNotifyId(notifyId, start, length);
        }
        return null;
    }

    /**
     * 获取被通知用户的所有的通知
     *
     * @param notifiedId
     * @return List<Notification>
     */
    public List<Notification> getNotificationListByNotifiedId(int notifiedId, int start, int length) {
        if (notifiedId > 0 && start>= 0 && length > 0){
            return iNotification.getNotificationListByNotifiedId(notifiedId, start,length);
        }
        return null;
    }

    /**
     * 获取被通知用户的所有的未读的通知
     *
     * @param notifiedId
     * @return List<Notification>
     */
    public List<Notification> getUnreadNotificationListByNotifiedId(int notifiedId, int start, int length) {
        if (notifiedId > 0 && start >= 0 && length > 0){
            return iNotification.getUnreadNotificationListByNotifiedId(notifiedId, start, length);
        }
        return null;
    }

    /**
     * 获取被通知用户的所有的未读消息的条数
     *
     * @param notifiedId
     * @return List<Notification>
     */
    @Override
    public List<Notification> getUnreadCountByNotifiedId(int notifiedId) {
        if (notifiedId > 0){
            return iNotification.getUnreadCountByNotifiedId(notifiedId);
        }
        return null;
    }

    /**
     * 创建一条新的通知
     *
     * @param notification
     */
    public void insertNotifications(Notification notification) {
        if (notification.getNotifyId() > 0
                && notification.getNotifyNick() != ""
                && notification.getNotifiedId() > 0
                && notification.getNotifiedNick() != ""
                && notification.getContent() != ""
                && notification.getStatus() == Notification.STATUS_UNREAD){
            iNotification.insertNotifications(notification);
        }

    }

    /**
     * 更新一条通知
     *
     * @param notification
     */
    public void updateNotifications(Notification notification) {
        if (notification.getStatus() == Notification.STATUS_READ){
            iNotification.updateNotifications(notification);
        }
    }

    /**
     * 删除一条通知
     *
     * @param notification
     */
    public void deleteNotifications(Notification notification) {
        if (notification.getId() > 0){
            iNotification.deleteNotifications(notification);
        }
    }
}
